
const port = 9000
import mqtt from "mqtt";
let opt = {
    "endpoint": "post-cn-apg3xckag01.mqtt.aliyuncs.com",
    "client_id": "GID_test@@@cd_62_f4_3d_b4_ba",
    "username": "Signature|LTAI5tF8J3CrdWmRiuTjxHbF|post-cn-apg3xckag01",
    "password": "ZniQJuMGL+eWdGdPqUBw/cYXPPA=",
    "publish_topic": "device-server",
    "subscribe_topic": "devices/cd_62_f4_3d_b4_ba"
}
opt = {
    "endpoint": "post-cn-apg3xckag01.mqtt.aliyuncs.com",
    "client_id": 'GID_test@@@28_37_2f_87_97_d0',
    "username": 'Signature|LTAI5tF8J3CrdWmRiuTjxHbF|post-cn-apg3xckag01',
    "password": 'Grwx1rLZBjF2PRUxU2EVTpWrAPw=',
    "publish_topic": "device-server",
    "subscribe_topic": "devices/28_37_2f_87_97_d0"
}
const client = mqtt.connect(`mqtts://${opt.endpoint}:8883`,{        
    clientId: opt.client_id,
    username: opt.username,
    password: opt.password,
    protocolVersion: 5, // 指定 MQTT 5.0
    rejectUnauthorized: false, // 要求服务器提供有效证书
    keepalive: 30,
});
// 连接成功回调
client.on('connect', () => {
    console.log(`MQTT 连接成功`);
    // const subscribe_topic = opt['subscribe_topic'].split("/")[0] + '/p2p/GID_test@@@' + MAC_ADDR.replace(/:/g, '_')
    // console.log(`Connected to MQTT broker (MQTT 5.0):${subscribe_topic}`);
    client.subscribe(opt['subscribe_topic'], { qos: 2 }, (err) => {
        if (!err) {
            console.log(`Subscribed to topic: ${opt['subscribe_topic']}`);
        } else {
            console.error('Subscription error:', err);
        }
    });
});

// 接收消息回调
client.on('message', (topic, message) => {
    console.log(`Received message on topic ${topic}: ${message.toString()}`);
});

// 错误处理
client.on('error', (err) => {
    console.error('MQTT Error:', err);
});

// 连接关闭
client.on('close', () => {
    console.log('Connection closed');
});

// 断开连接
client.on('offline', () => {
    console.log('Client is offline');
});


console.log(`${port}sss`)